package com.bstek.poc;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.bstek.poc.service.HeartbeatService;
import com.bstek.poc.utils.VPNUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * ┃　　　┃   神兽保佑
 * ┃　　　┃   代码无BUG！
 * ┃　　　┗━━━┓
 * ┃　　　　　　　┣┓
 * ┃　　　　　　　┏┛
 * ┗┓┓┏━┳┓┏┛
 * ┃┫┫　┃┫┫
 * ┗┻┛　┗┻┛
 * Created by leelucifer on 15/1/19.
 */
public class WebActivity extends Activity {
    public static boolean onrotaion=true;
    WebView webView;
    String uri;
    Activity activity;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity=this;
        if (onrotaion) {
            setContentView(R.layout.activity_web);

            webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setSupportMultipleWindows(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webView.requestFocus();

            String user=getSharedPreferences("VPN_POC",MODE_PRIVATE).getString("user","");
            uri = "http://19.131.100.224:8080/zhcx/gov.sscz.zhsp.touch.view.Main.d?user="+user;

            try {
                PackageInfo info=getPackageManager().getPackageInfo(getPackageName(), 0);
                String versionName=info.versionName;
                uri+="&version=android_"+versionName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                    if(errorCode==-8){
                        startActivity(new Intent(activity,CordovaApp.class).putExtra("param",1));
                        finish();
                    }
                }

            });
            webView.loadUrl(uri);

            Intent heartbeatService = new Intent(this, HeartbeatService.class);
            startService(heartbeatService);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //Change flag  here
        onrotaion=false;
    }

    long waitTime = 2000;
    long touchTime = 0;
    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if((currentTime-touchTime)>=waitTime) {
            Toast.makeText(this, "再按一次退出登录", Toast.LENGTH_SHORT).show();
            touchTime = currentTime;
        }else {
            finish();
        }
    }

    BroadcastReceiver sessionOutReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            startActivity(new Intent(activity,CordovaApp.class).putExtra("param",1));
            finish();
        }
    };

    @Override
    protected void onStart() {
        registerReceiver(sessionOutReceiver, new IntentFilter(HeartbeatService.SESSION_EXCPETION));
        super.onStart();
    }

    @Override
    protected void onStop() {
        unregisterReceiver(sessionOutReceiver);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        stopService(new Intent(this,HeartbeatService.class));
        destroyVPN();
        super.onDestroy();
    }

    private void destroyVPN(){
        VPNUtils.closeVPN(this);
    }
}